#include <stdio.h>   /* required for file operations */
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "libSbNet.h"


int main(int argc, char *argv[])
{
	int retValue = 0; 
	if(argc < 2) {
		printf("Incorrect number of args\n");
		printf("Args: configuration file\n");
		retValue = 1;
	}


	SB_NETCACHE* dataCache = NULL;

  	retValue=url_loadData(&dataCache,argv[1]);
	

	SB_NETCACHE *cursor=dataCache;

	/*test*/
	while (cursor!=NULL){
		SB_NETCACHE* loop=malloc(sizeof(SB_NETCACHE));
		loop=cursor;
		char *pch=strrchr(loop->url,'/');
		char filename[30]="";
		strcpy(filename,pch+1);
		FILE *out=fopen(filename,"w+");
		if(out==NULL) {
	    		perror("couldn't open output file\n");
	  		printf( "Value of errno: %d\n", errno );
		}else{
			fwrite(loop->cacheBuffer,1,loop->dataLength,out);
		}
		cursor=cursor->next;
	}

	return retValue;
}

